home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / ZDEVICE < prev    next >
Text File  |  1991-10-25  |  6KB  |  218 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zdevice.c */
  21. /* Device-related operators for GhostScript */
  22. #include "ghost.h"
  23. #include "alloc.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "state.h"
  27. #include "gsmatrix.h"
  28. #include "gsstate.h"
  29. #include "gxdevice.h"
  30. #include "store.h"
  31.  
  32. /* copypage */
  33. int
  34. zcopypage(register os_ptr op)
  35. {    return gs_copypage(igs);
  36. }
  37.  
  38. /* copyscanlines */
  39. int
  40. zcopyscanlines(register os_ptr op)
  41. {    os_ptr op1 = op - 1;
  42.     os_ptr op2 = op - 2;
  43.     gx_device *dev;
  44.     int code;
  45.     uint bytes_copied;
  46.     check_type(*op2, t_device);
  47.     dev = op2->value.pdevice;
  48.     check_type(*op1, t_integer);
  49.     if ( op1->value.intval < 0 || op1->value.intval > dev->height )
  50.         return e_rangecheck;
  51.     check_write_type(*op, t_string);
  52.     code = gs_copyscanlines(dev, (int)op1->value.intval,
  53.         op->value.bytes, r_size(op), NULL, &bytes_copied);
  54.     if ( code < 0 ) return e_typecheck;    /* not a memory device */
  55.     *op2 = *op;
  56.     r_set_subrange_size(op2, bytes_copied);
  57.     pop(2);
  58.     return 0;
  59. }
  60.  
  61. /* currentdevice */
  62. int
  63. zcurrentdevice(register os_ptr op)
  64. {    gx_device *dev = gs_currentdevice(igs);
  65.     push(1);
  66.     make_tv(op, t_device, pdevice, dev);
  67.     return 0;
  68. }
  69.  
  70. /* devicename */
  71. int
  72. zdevicename(register os_ptr op)
  73. {    char *dname;
  74.     int code;
  75.     check_type(*op, t_device);
  76.     dname = gs_devicename(op->value.pdevice);
  77.     code = string_to_ref(dname, op, "devicename");
  78.     if ( code < 0 ) return code;
  79.     return 0;
  80. }
  81.  
  82. /* deviceparams */
  83. int
  84. zdeviceparams(register os_ptr op)
  85. {    int code = write_matrix(op);
  86.     int width, height;
  87.     if ( code < 0 ) return code;
  88.     check_type(op[-1], t_device);
  89.     gs_deviceparams(op[-1].value.pdevice, (gs_matrix *)op->value.refs,
  90.             &width, &height);
  91.     push(2);
  92.     make_mark(op - 3);
  93.     make_int(op - 1, width);
  94.     make_int(op, height);
  95.     return 0;
  96. }
  97.  
  98. /* flushpage */
  99. int
  100. zflushpage(register os_ptr op)
  101. {    return gs_flushpage(igs);
  102. }
  103.  
  104. /* getdevice */
  105. int
  106. zgetdevice(register os_ptr op)
  107. {    gx_device *dev;
  108.     check_type(*op, t_integer);
  109.     if ( op->value.intval != (int)(op->value.intval) )
  110.         return e_rangecheck;    /* won't fit in an int */
  111.     dev = gs_getdevice((int)(op->value.intval));
  112.     if ( dev == 0 ) return e_rangecheck;    /* index out of range */
  113.     make_tv(op, t_device, pdevice, dev);
  114.     return 0;
  115. }
  116.  
  117. /* makedevice */
  118. int
  119. zmakedevice(register os_ptr op)
  120. {    gs_matrix imat;
  121.     gx_device *new_dev;
  122.     int code;
  123.     check_type(op[-3], t_device);
  124.     check_type(op[-1], t_integer);    /* width */
  125.     check_type(*op, t_integer);    /* height */
  126.     if (    (ulong)(op[-1].value.intval) > max_uint >> 1 ||
  127.         (ulong)(op->value.intval) > max_uint >> 1
  128.        ) return e_rangecheck;
  129.     if ( (code = read_matrix(op - 2, &imat)) < 0 ) return code;
  130.     /* Everything OK, create device */
  131.     code = gs_makedevice(&new_dev, op[-3].value.pdevice, &imat,
  132.                  (int)op[-1].value.intval, (int)op->value.intval,
  133.                  alloc);
  134.     if ( code == 0 )
  135.        {    make_tv(op - 3, t_device, pdevice, new_dev);
  136.         pop(3);
  137.        }
  138.     return code;
  139. }
  140.  
  141. /* makeimagedevice */
  142. int
  143. zmakeimagedevice(register os_ptr op)
  144. {    gs_matrix imat;
  145.     gx_device *new_dev;
  146.     float colors[256 * 3];
  147.     int num_colors;
  148.     int code;
  149.     check_type(op[-2], t_integer);    /* width */
  150.     check_type(op[-1], t_integer);    /* height */
  151.     if ( r_has_type(op, t_null) )    /* true color */
  152.        {    num_colors = -24;    /* 24-bit true color */
  153.        }
  154.     else
  155.        {    check_array(*op);    /* palette */
  156.         num_colors = r_size(op);
  157.        }
  158.     if (    (ulong)(op[-2].value.intval) > max_uint >> 1 ||
  159.         (ulong)(op[-1].value.intval) > max_uint >> 1 ||
  160.         num_colors > 256
  161.        ) return e_rangecheck;
  162.     if ( (code = read_matrix(op - 3, &imat)) < 0 ) return code;
  163.     /* Check and convert colors */
  164.        {    int i;
  165.         ref *pc = op->value.refs;
  166.         float *p = colors;
  167.         for ( i = 0; i < num_colors; i++, pc++, p += 3 )
  168.            {    check_type(*pc, t_color);
  169.             code = gs_colorrgb(pc->value.pcolor, p);
  170.             if ( code < 0 ) return code;
  171.            }
  172.        }
  173.     /* Everything OK, create device */
  174.     code = gs_makeimagedevice(&new_dev, &imat,
  175.                   (int)op[-2].value.intval,
  176.                   (int)op[-1].value.intval,
  177.                   colors, num_colors, alloc);
  178.     if ( code == 0 )
  179.        {    make_tv(op - 3, t_device, pdevice, new_dev);
  180.         pop(3);
  181.        }
  182.     return code;
  183. }
  184.  
  185. /* nulldevice */
  186. int
  187. znulldevice(register os_ptr op)
  188. {    gs_nulldevice(igs);
  189.     return 0;
  190. }
  191.  
  192. /* setdevice */
  193. int
  194. zsetdevice(register os_ptr op)
  195. {    int code;
  196.     check_type(*op, t_device);
  197.     code = gs_setdevice(igs, op->value.pdevice);
  198.     if ( code == 0 ) pop(1);
  199.     return code;
  200. }
  201.  
  202. /* ------ Initialization procedure ------ */
  203.  
  204. op_def zdevice_op_defs[] = {
  205.     {"0copypage", zcopypage},
  206.     {"3copyscanlines", zcopyscanlines},
  207.     {"0currentdevice", zcurrentdevice},
  208.     {"1devicename", zdevicename},
  209.     {"1deviceparams", zdeviceparams},
  210.     {"0flushpage", zflushpage},
  211.     {"1getdevice", zgetdevice},
  212.     {"4makedevice", zmakedevice},
  213.     {"4makeimagedevice", zmakeimagedevice},
  214.     {"0nulldevice", znulldevice},
  215.     {"1setdevice", zsetdevice},
  216.     op_def_end(0)
  217. };
  218.